package ui.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.peng.auctionsystem.R;

import base.BaseFragment;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import entity.LoginEntity;
import ui.activity.LoginActivity;
import ui.activity.LotDetailActivity;
import ui.activity.ModifyInfoActivity;
import utils.Constants;
import utils.GlobalDataUtils;


/**
 * “我的”界面
 */
public class MyFragment extends BaseFragment {

    @Bind(R.id.my_iv_avatar)
    ImageView myIvAvatar;
    @Bind(R.id.my_tv_nickname)
    TextView myTvNickname;
    @Bind(R.id.my_iv_sex)
    ImageView myIvSex;
    @Bind(R.id.my_rl_publish)
    RelativeLayout myRlPublish;
    @Bind(R.id.my_rl_pass_in)
    RelativeLayout myRlPassIn;
    @Bind(R.id.my_rl_pass_out)
    RelativeLayout myRlPassOut;
    @Bind(R.id.my_rl_join)
    RelativeLayout myRlJoin;
    @Bind(R.id.my_tv_logout)
    TextView myTvLogout;
    @Bind(R.id.my_rl_info)
    RelativeLayout myRlInfo;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_my;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);

        LoginEntity user = GlobalDataUtils.getCurUser();
        Glide.with(this).load(user.getData().getPicturePath()).into(myIvAvatar);
        myTvNickname.setText(user.getData().getName());
        if (user.getData().getSex().equals("1")) {
            myIvSex.setImageResource(R.mipmap.sex_male);
        } else {
            myIvSex.setImageResource(R.mipmap.sex_female);
        }

        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.my_rl_publish, R.id.my_rl_pass_in, R.id.my_rl_pass_out, R.id.my_rl_join, R.id.my_tv_logout, R.id.my_rl_info})
    public void onViewClicked(View view) {
        Intent intent = null;
        intent = new Intent(getActivity(), LotDetailActivity.class);
        switch (view.getId()) {
            case R.id.my_rl_publish:// 我的发布
                intent.putExtra("datatype", Constants.MY_LOT);
                startActivity(intent);
                break;
            case R.id.my_rl_pass_in:// 流拍
                intent.putExtra("datatype", Constants.PASS_IN_LOT);
                startActivity(intent);
                break;
            case R.id.my_rl_pass_out:// 已拍出
                intent.putExtra("datatype", Constants.PASS_OUT_LOT);
                startActivity(intent);
                break;
            case R.id.my_rl_join:// 我参与的竞品
                intent.putExtra("datatype", Constants.MY_JOIN_LOT);
                startActivity(intent);
                break;
            case R.id.my_tv_logout:// 退出登录

                Intent logoutIntent = new Intent(getActivity(), LoginActivity.class);
                startActivity(logoutIntent);
                getActivity().finish();
                break;
            case R.id.my_rl_info:// 修改个人信息
                Intent modifyIntent = new Intent(getActivity(), ModifyInfoActivity.class);
                startActivityForResult(modifyIntent, 1000);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1000) {
            LoginEntity user = GlobalDataUtils.getCurUser();
            Glide.with(this).load(user.getData().getPicturePath()).into(myIvAvatar);
            myTvNickname.setText(user.getData().getName());
            if (user.getData().getSex().equals("1")) {
                myIvSex.setImageResource(R.mipmap.sex_male);
            } else {
                myIvSex.setImageResource(R.mipmap.sex_female);
            }
        }
    }
}
